#!/usr/bin/python3
# coding=utf-8

import os
import yaml

ROOT = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]
CONFIG_FILE = os.path.join(ROOT, "configs", "settings.yml")

def get_config_info(target_file=CONFIG_FILE):
    if os.path.exists(target_file):
        with open(target_file, "rb") as f:
            # data = list(yaml.safe_load_all(f))
            data = yaml.safe_load(f)
        
        return data

class YAMLReader(object):
    # Only for parse YAML file as configuration, and return YAML File Content as List

    def __init__(self, target_file):
        """Read YAML File
        :param target_file: read YAML file
        :type target_file: string
        """
        if os.path.exists(target_file):
            self.yaml_obj = target_file

        else:
            raise FileNotFoundError("File not found!")

        self._data = None

    @property
    def data(self):
        """read yaml file and convert to List
        :return data: read from yaml file
        :rtype data: list
        """
        if not self._data:
            with open(self.yaml_obj, "rb") as f:
                # 将生成器转换成List, 方便后续操作
                self._data = list(yaml.safe_load_all(f))

        return self._data


class ConfigHelper(object):
    def __init__(self, target_file=CONFIG_FILE):
        self.yaml_data = YAMLReader(target_file).data if target_file else None

    def get(self, key, index=0):
        return self.yaml_data[index].get(key) if self.yaml_data else None


Global_Params = ConfigHelper(CONFIG_FILE)
Global_Params_Info = get_config_info(CONFIG_FILE)

if __name__ == "__main__":
    # print(ConfigHelper().get("FAT"))
    exec_vars = get_config_info()
    # print(exec_vars)
    print(exec_vars.get("SIT").get("acct"))



